package com.sky.mapper.common;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.pojo.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrdersVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface OrderMapper {
    void insert(Orders order);

    @Select("select * from orders where number = #{orderNumber}")
    Orders selectByNumber(String orderNumber);

    @Update("update orders set pay_status=#{payStatus}, status=#{status}, checkout_time=#{checkoutTime} where number = #{number}")
    void updateByOutTradeNo(Orders orders);

    @Update("update orders set status=#{status}, cancel_time=#{cancelTime}, cancel_reason=#{cancelReason} where id = #{id}")
    void updateById(Orders orders);

    @Select("select * from orders where order_time < #{dateTime} and status = #{status}")
    List<Orders> selectByStatusAndTime(@Param("dateTime") LocalDateTime dateTime,@Param("status") Integer status);

    @Select("select * from orders where order_time > #{dateTime} and status = #{status}")
    List<Orders> selectByStatusAndBeginTime(@Param("dateTime") LocalDateTime dateTime,@Param("status") Integer status);

    Page<OrdersVO> pageQuery(OrdersPageQueryDTO queryDTO);

    @Select("select count(*) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select status,count(*) total from orders group by status")
    List<Map> groupByStatus();

    OrderStatisticsVO groupByStatus1();

    OrdersVO getDetailById(Long orderId);

    @Select("select * from orders where id = #{id}")
    Orders selectById(Long id);

    /**
     * 根据用户的id和状态查询历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageOrderQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    @Update("update orders set status = #{status} where number = #{number}")
    void updateStatusByNumber(Orders orders);

    @Update("update orders set status = #{status}, delivery_time=#{deliveryTime} where number = #{number}")
    void updateStatusAndDeliveryTimeByNumber(Orders orders);

    @Select("select * from orders where number = (select order_number from tb_courier_trade_order where trade_no = #{tradeNo})")
    Orders selectByTradeNo(String tradeNo);

    @Select("select sum(amount) from orders where status = 6 and order_time > concat(#{date}, ' 00:00:00') and order_time < concat(#{date}, ' 23:59:59')")
    Double countAmountByDay(String date);

    Map countAmountByDay2(@Param("dateList") List<String> dateList,
                          @Param("begin") String begin,
                          @Param("end") String end);

    Integer countNumberByTime(@Param("beginTime") String beginTime,
                              @Param("end") LocalDate end,
                              @Param("status") Integer status);

    Map<String, Object> countNumberByDay(@Param("beginTime") String beginTime,
                                         @Param("end") LocalDate end,
                                         @Param("dateList") List<String> dateList,
                                         @Param("status") Integer status);

    int selectOrdersByDateAndStatus(@Param("begin") String beginStr,
                                    @Param("end") String endStr,
                                    @Param("status") Integer status);

    Map<String, Object> selectSalesVolumesByDay3(@Param("begin") String oldBegin,
                                                 @Param("end") String oldEnd,
                                                 @Param("list") List<String> dataList);
}
